//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.util.io;

import java.awt.Robot;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.Arrays;
import java.util.logging.Logger;

public class PasswordField {
    private static Logger logger = Logger.getLogger(PasswordField.class.getName());

    public PasswordField() {
    }

    public static final char[] getPassword(String prompt) throws IOException {
        return getPassword(System.in, prompt);
    }

    public static final char[] getPassword(InputStream in, String prompt) throws IOException {
        MaskingThread maskingthread = new MaskingThread(prompt);
        Thread thread = new Thread(maskingthread);
        thread.start();
        char[] lineBuffer;
        char[] buf = lineBuffer = new char[128];
        int room = buf.length;
        int offset = 0;

        label43:
        while(true) {
            int c = ((InputStream)in).read();
            switch(c) {
            case -1:
            case 10:
                break label43;
            case 13:
                int c2 = ((InputStream)in).read();
                if (c2 == 10 || c2 == -1) {
                    break label43;
                }

                if (!(in instanceof PushbackInputStream)) {
                    in = new PushbackInputStream((InputStream)in);
                }

                ((PushbackInputStream)in).unread(c2);
            default:
                --room;
                if (room < 0) {
                    buf = new char[offset + 128];
                    room = buf.length - offset - 1;
                    System.arraycopy(lineBuffer, 0, buf, 0, offset);
                    Arrays.fill(lineBuffer, ' ');
                    lineBuffer = buf;
                }

                buf[offset++] = (char)c;
            }
        }

        maskingthread.stopMasking();
        System.out.print("\b");
        String os = System.getProperty("os.name");
        if (os != null && os.toLowerCase().startsWith("windows")) {
            try {
                Robot robot = new Robot();
                robot.keyPress(18);
                robot.keyPress(118);
                robot.keyRelease(118);
                robot.keyRelease(18);
            } catch (Exception var12) {
                logger.warning("Could not clears command history: " + var12);
            }
        }

        if (offset == 0) {
            return null;
        } else {
            char[] ret = new char[offset];
            System.arraycopy(buf, 0, ret, 0, offset);
            Arrays.fill(buf, ' ');
            return ret;
        }
    }
}
